<?php
$products = $params['products'];
?>
<style type="text/css">
.dropdown-menu a {
    cursor: pointer;
}

.payment-button-block {
    text-align: right;
}
</style>
<div class="pull-left span9">
	<table class="table">
		<thead>
			<tr>
				<th>บริการ</th>
				<th>กำหนดชำระ/หมดอายุ</th>
				<th>ยอดชำระ</th>
				<th>สถานะ</th>
                <th></th>
			</tr>
		</thead>
		<?php
        foreach ($products as $key => $product){
        switch($product['invoice']['status']){
            case Invoice::STATUS_PAY: $status = 'ปกติ'; break;
            case Invoice::STATUS_CHECK: $status = 'รอตรวจสอบ' ; break;
            default: $status = 'ยังไม่ได้ชำระ'; break;
        }
        if(!isset($product['invoice']['status'])){
            $status = 'ปกติ';
        }
        $service = isset($product['package']);
        ?>
		<tbody>
			<tr>
				<td>
                    <strong><?php echo $product['package']['type'];?></strong><br />
                    <?php echo $product['package']['name'];?>
                </td>
				<td><?php
                    $dateTime = isset($product['id'])
                        ? new DateTime($product['dateTimeout'])
                        : new DateTime($product['invoice']['dateDue']);
                    echo $dateTime->format('j/n/Y');?>
                </td>
				<td><?php echo isset($product['invoice']['price'])? Helper::formatMoney($product['invoice']['price']).' บาท': '-';?></td>
                <td>
                    <?php echo $status;?>
                    <?php if(isset($product['invoice']['status']) && $product['invoice']['status']==0){?>
                    <br />
                    <button type="button" class="btn btn-warning btn-invoice">แจ้งชำระ</button>
                    <input type="checkbox" class="checkbox-invoice" value="<?php echo $product['invoice']['id'];?>" style="display: none;" />
                    <?php }?>
                </td>
                <td>
                    <div class="btn-group">
                        <a class="btn dropdown-toggle action" data-toggle="dropdown" href="#">
                            Action
                            <span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <?php if(isset($product['id'])){?>
                            <li><a href="index.php?page=user/product.invoices&productId=<?php echo $product['id'];?>">การชำระค่าบริการ</a></li>
                            <li class="divider"></li>
                            <li><a href="index.php?page=product&id=<?php echo $product['id'];?>">รายละเอียด</a></li>
                            <?php }else{?>
                            <li><a href="index.php?page=invoice&id=<?php echo $product['invoice']['id'];?>">รายละเอียดการสั่งซื้อ</a></li>
                            <?php }?>
                        </ul>
                    </div>
                </td>
			</tr>
		</tbody>
		<?php }?>
	</table>
    <div class="payment-button-block">
        <button class="btn btn-primary fade payment-button">ต่อไป</button>
    </div>
</div>
<div class="clear"></div>
<script type="text/javascript">
$(function(){
    $('.btn-invoice').click(function(event){
        $(this).toggleClass('active');
        var ck = $(this).next();
        ck.attr("checked", $(this).hasClass('active'));
        if($('.checkbox-invoice:checked').size()>0){
            $('.payment-button').addClass('in');
        }else {
            $('.payment-button').removeClass('in');
        }
    });
    $('.payment-button').click(function(event){
        var form = $('<form method="get" action="index.php"><input type="text" name="page" value="payment" /></form>');
        var arr = [];
        $('.checkbox-invoice:checked').each(function(index, el){
            arr.push($(el).val());
        });
        form.append('<input type="text" name="invoiceId" value="'+arr.join(',')+'" />');
        form.submit();
    });
});
</script>